/**
 * 
 */
package com.ideeli.webextractor.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ideeli.webextractor.dao.ProductDao;
import com.ideeli.webextractor.domain.Headers;
import com.ideeli.webextractor.domain.Product;
import com.ideeli.webextractor.utils.Utility;

/**
 * @author spaida
 *
 */
public class EbMappingServiceImpl implements EbMappingService {
	
	private static final Logger logger = Logger.getLogger(EbMappingServiceImpl.class);
	
	private GenerateCSVService generateCSVService ;

	private ProductDao productDao;
	
	private CacheService cacheService;
	
	private int fileGenCounter=0;
	
	
	public void mapEbFields(List<Product> products) throws Exception
	{
		if(fileGenCounter==0)
			generateCSVService.generateDir();
		
		fileGenCounter++;
		logger.warn("****Generating file no: "+fileGenCounter);
		
		List<Map<Headers, String>> productList=new ArrayList<Map<Headers, String>>();
		
		for(Product product:products)
		{
			Map<Headers, String> prodFields=new HashMap<Headers,String>();
			mapProductFields(prodFields,product);
			productList.add(prodFields);
		}
				
		generateCSVService.generateCSV(productList);
		
		logger.warn("Start mapEbFields");
	}

	private void mapProductFields(Map<Headers, String> prodFields, Product product) 
	{
		prodFields.put(Headers.Action, "Add");
		prodFields.put(Headers.SiteID, "Singapore");
		prodFields.put(Headers.Format, "FixedPriceItem");
		
		
		prodFields.put(Headers.Title,mapTitle(product.getName()));
		prodFields.put(Headers.Condition, "1000");
		prodFields.put(Headers.Category, cacheService.getProdIDebCatCode(product.getProduct_id()));
		
		prodFields.put(Headers.PicURL, "http://itshop.com.sg/image/"+product.getImage());
//		http://itshop.com.sg/image/data/images/large/1200687.jpg
		
		//TODO map category 2
//		prodFields.put(Headers.Category2, cacheService.getProdIDebCatCode(product.getProduct_id()));
		
		prodFields.put(Headers.StoreCategory, "0");
		prodFields.put(Headers.StoreCategory2, "0");
		prodFields.put(Headers.Quantity, "1");
		prodFields.put(Headers.Currency, "SGD");
		prodFields.put(Headers.StartPrice,mapPrice(product.getPrice())+"");
		prodFields.put(Headers.Duration, "3");
		prodFields.put(Headers.PrivateAuction, "0");
		prodFields.put(Headers.Country, "SG");
		
		//TODO map Description
		prodFields.put(Headers.Description,"@@@@%<FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>%0D%0A<P><FONT size=4>Alternatively you can check out at&nbsp;our website:</FONT></P>%0D%0A<P><FONT size=4>http://itshop.com.sg/index.php?route=product/product&amp;product_id="+product.getProduct_id()+"</FONT></P></FONT></FONT>");//+ product.getDescription());
		prodFields.put(Headers.BoldTitle, "0");
		prodFields.put(Headers.Featured, "0");
		prodFields.put(Headers.GalleryType, "None");
		
		prodFields.put(Headers.Highlight, "0");
		prodFields.put(Headers.Border, "0");
		prodFields.put(Headers.HomePageFeatured, "0");
		prodFields.put(Headers.Subtitleinsearchresutls, "0");
		prodFields.put(Headers.Location, "Singapore");
		prodFields.put(Headers.PostalCode, "079903");
		prodFields.put(Headers.ImmediatePayRequired, "0");
		prodFields.put(Headers.GiftIcon, "0");
		
		
		
		//TODO remove below later
		prodFields.put(Headers.PayPalEmailAddress, "sandip.paida@ideelitrading.com"); //not required
//		http://itshop.com.sg/index.php?route=product/product&product_id=4398
//		String pay="Online/ATM/Bank Transer Instructions/cash desposit/cheque deposit:%0D%0A%0D%0ADBS Current a/c : 017-903393-4%0D%0AAccount Name: ideeli trading private limited%0D%0ABank Code   : 7171%0D%0A%0D%0AGoods will be dispatched after the payment is cleared. Usually transfer from the same bank is instantly from other banks it will take upto 2 working days. %0D%0A%0D%0Ashop more at : www.itshop.com.sg";
//		prodFields.put(Headers.PaymentInstructions, "@@@@%alternatively you can check out at my website:%0D%0A%0D%0Ahttp://itshop.com.sg/index.php?route=product/product&product_id="+product.getProduct_id()
//				+"%0D%0A%0D%0A%0D%0Ato find out more products please visit:%0D%0A%0D%0Awww.itshop.com.sg  "+pay);
		
		prodFields.put(Headers.PaymentInstructions, "@@@@%Online/ATM/Bank Transer Instructions/cash desposit/cheque deposit:%0D%0A%0D%0ADBS Current a/c : 017-903393-4%0D%0AAccount Name: ideeli trading private limited%0D%0ABank Code   : 7171%0D%0A%0D%0AGoods will be dispatched after the payment is cleared. Usually transfer from the same bank is instantly from other banks it will take upto 2 working days. %0D%0A%0D%0Ashop more at : www.itshop.com.sg");

		prodFields.put(Headers.UPC, product.getUpc());
		
		prodFields.put(Headers.ShippingType, "Flat");
		
		//Payment methods
		prodFields.put(Headers.MOCashiers, "1");
		prodFields.put(Headers.PersonalCheck, "1");
		prodFields.put(Headers.MoneyXferAccepted, "1");
		prodFields.put(Headers.PaymentSeeDescription, "1");
		prodFields.put(Headers.GlobalShippingService, "0");
		prodFields.put(Headers.ShippingService1Option, "SG_DomesticSpeedpostIslandwide");
		prodFields.put(Headers.ShippingService1Cost, "0.00");
		prodFields.put(Headers.ShippingService1Priority, "1");
		prodFields.put(Headers.ShippingService1FreeShipping, "1");
		prodFields.put(Headers.GetItFast, "0");
		prodFields.put(Headers.DispatchTimeMax, "1");
		prodFields.put(Headers.ValuePackBundle, "0");
		prodFields.put(Headers.BuyerRequirementsLinkedPayPalAccount, "0");
		prodFields.put(Headers.ShippingDiscountProfileID, "0||");
		prodFields.put(Headers.InternationalShippingDiscountProfileID, "0||");
		prodFields.put(Headers.ApplyProfileDomestic, "0");
		prodFields.put(Headers.ApplyProfileInternational, "0");
		prodFields.put(Headers.DomesticInsuranceFee, "0");
		
		prodFields.put(Headers.ReturnsAcceptedOption, "ReturnsNotAccepted");
		
		//TODO check label tag C:Microsoft Office 2011 ==	GZA-00136
	
	}

	private String mapTitle(String title)
	{
		
		
		if(title!=null )
		{
			title=title.replaceAll(",", "");
			if(title.length()>78)
				title=title.substring(0, 79).trim();
		}
			
		
		return title;
	}

	private float mapPrice(float price)
	{
		
		//logic specific to <= 100 shipping cost $5
		if(price<=100)
		{
			price+=5;
		}
		price+=2.0;
		return Utility.round2(price);
	}

	public ProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	
	public GenerateCSVService getGenerateCSVService() {
		return generateCSVService;
	}

	public void setGenerateCSVService(GenerateCSVService generateCSVService) {
		this.generateCSVService = generateCSVService;
	}

	public CacheService getCacheService() {
		return cacheService;
	}

	public void setCacheService(CacheService cacheService) {
		this.cacheService = cacheService;
	}


}
